package api

import (
	"GopherFocus/app/model"
	"GopherFocus/app/system/index/internal/define"
	"GopherFocus/app/system/index/internal/service"
	"github.com/gogf/gf/net/ghttp"
)

// 主题管理
var Topic = topicApi{}

type topicApi struct{}

// 展示主题首页
func (a *topicApi) Index(r *ghttp.Request) {
	var (
		// 为什么要这样定义
		req *define.ContentGetListReq
	)
	if err := r.Parse(&req); err != nil {
		service.View.Render500(r, model.View{
			Error: err.Error(),
		})
	}
	// req.Type
	// 为什么要在model中定义???
	req.Type = model.ContentTypeTopic
	if getLisRes, err := service.Content.GetList(r.Context(), req.ContentGetListInput); err != nil {
		service.View.Render500(r, model.View{
			Error: err.Error(),
		})
	} else {
		// 获取分级标题： 例如点击"主题"下的"分享"，则在浏览页标签上会显示 "分享-主题-focus聚焦社区"
		title := service.View.GetTitle(r.Context(), &define.ViewGetTitleInput{
			ContentType: req.Type,
			CategoryId:  req.CategoryId,
		})
		service.View.Render(r, model.View{
			ContentType: req.Type,
			Data:        getLisRes,
			Title:       title,
		})
	}
}

// 展示主题详情
func (a *topicApi) Detail(r *ghttp.Request) {
	var (
		data *define.ContentDetailReq
	)
	if err := r.Parse(&data); err != nil {
		service.View.Render500(r, model.View{
			Error: err.Error(),
		})
	}
	// 根据content id获取Content和User内容
	if getDetailRes, err := service.Content.GetDetail(r.Context(), data.Id); err != nil {
		service.View.Render500(r)
	} else {
		if getDetailRes == nil {
			service.View.Render404(r)
		}
		// 面包屑导航用于告诉用户当前位于网站哪个位置以及如果返回（源自于在森林中用面包屑引导回家的路）
		// 在详情页左上角显示为：首页/主题/翻译/内容详情, 其中首页/主题/翻译分别对应一个链接，点击链接即可返回到对应位置
		// 第一级是首页 第二级是主题(通过contentType获取) 第三级是具体的栏目(category，通过categoryId获取) 第四级是内容详情
		_ = service.Content.AddViewCount(r.Context(), data.Id, 1)
		service.View.Render(r, model.View{
			ContentType: model.ContentTypeTopic,
			Data:        getDetailRes,
			Title: service.View.GetTitle(r.Context(), &define.ViewGetTitleInput{
				ContentType: getDetailRes.Content.Type,
				CategoryId:  getDetailRes.Content.CategoryId,
				CurrentName: getDetailRes.Content.Title,
			}),
			// 英[ˈbrɛdkrʌm]
			// ContentType用于获取主题的name和uri
			// CategoryId用于获取栏目的name和uri
			// ContentId用于判断是否添加内容详情对应的面包屑
			BreadCrumb: service.View.GetBreadCrumb(r.Context(), &define.ViewGetBreadCrumbInput{
				ContentId:   getDetailRes.Content.Id,
				ContentType: getDetailRes.Content.Type,
				CategoryId:  getDetailRes.Content.CategoryId,
			}),
		})
	}
}

/*
func (a *topicApi) Detail(r *ghttp.Request) {
	var (
		data *define.ContentDetailReq
	)
	if err := r.Parse(&data); err != nil {
		service.View.Render500(r, model.View{
			Error: err.Error(),
		})
	}
	if getDetailRes, err := service.Content.GetDetail(r.Context(), data.Id); err != nil {
		service.View.Render500(r)
	} else {
		if getDetailRes == nil {
			service.View.Render404(r)
		}
		service.Content.AddViewCount(r.Context(), data.Id, 1)
		service.View.Render(r, model.View{
			ContentType: model.ContentTypeTopic,
			Data:        getDetailRes,
			Title: service.View.GetTitle(r.Context(), &define.ViewGetTitleInput{
				ContentType: getDetailRes.Content.Type,
				CategoryId:  getDetailRes.Content.CategoryId,
				CurrentName: getDetailRes.Content.Title,
			}),
			BreadCrumb: service.View.GetBreadCrumb(r.Context(), &define.ViewGetBreadCrumbInput{
				ContentId:   getDetailRes.Content.Id,
				ContentType: getDetailRes.Content.Type,
				CategoryId:  getDetailRes.Content.CategoryId,
			}),
		})
	}
}

*/
